#include "drawAPP.h"
#include "../../../lvgl/lvgl.h"

lv_obj_t *ui_ImgButtonPenToDet;
lv_obj_t *canvas;
lv_obj_t *ui_eraserBtn;
static lv_color_t cbuf[960 * 555];
static int color_now = 6;
static int pen_size = 5;
static bool is_erasing = false;
static lv_point_t last_point = {-1, -1};

// 函数声明
static void color_btn_event_cb(lv_event_t *e);
static void size_btn_event_cb(lv_event_t *e);
static void eraser_btn_event_cb(lv_event_t *e);
static void canvas_event_cb(lv_event_t *e);
static void draw_point(lv_obj_t *canvas, lv_coord_t x, lv_coord_t y);
static void draw_line(lv_obj_t *canvas, lv_coord_t x1, lv_coord_t y1, lv_coord_t x2, lv_coord_t y2);
static void create_color_buttons(lv_obj_t *parent);
static void create_size_buttons(lv_obj_t *parent);
static void create_eraser_button(lv_obj_t *parent);

// 颜色数组
static lv_color_t colors[15];
void init_colors(void)
{
    colors[0] = lv_color_hex(0x000000);
    colors[1] = lv_color_hex(0xFF0000);
    colors[2] = lv_color_hex(0xFF69B4);
    colors[3] = lv_color_hex(0xFF00FF);
    colors[4] = lv_color_hex(0x800080);
    colors[5] = lv_color_hex(0x0000FF);
    colors[6] = lv_color_hex(0x00BFFF);
    colors[7] = lv_color_hex(0x00FFFF);
    colors[8] = lv_color_hex(0x00FF00);
    colors[9] = lv_color_hex(0xADFF2F);
    colors[10] = lv_color_hex(0xFFFF00);
    colors[11] = lv_color_hex(0xFFA500);
    colors[12] = lv_color_hex(0x778899);
    colors[13] = lv_color_hex(0xD2691E);
    colors[14] = lv_color_hex(0xFFFFFF);
}

// 创建颜色选择按钮
static void create_color_buttons(lv_obj_t *parent)
{
    lv_obj_t *btn;
    int i;
    
    for(i = 0; i < 15; i++) {
        btn = lv_btn_create(parent);
        lv_obj_set_size(btn, 64, 45);
        lv_obj_set_pos(btn, 64 * i, 555);
        lv_obj_set_style_bg_color(btn, colors[i], LV_PART_MAIN | LV_STATE_DEFAULT);
        lv_obj_set_style_bg_opa(btn, LV_OPA_COVER, LV_PART_MAIN | LV_STATE_DEFAULT);
        lv_obj_set_style_radius(btn, 15, LV_PART_MAIN | LV_STATE_DEFAULT);
        lv_obj_add_event_cb(btn, color_btn_event_cb, LV_EVENT_CLICKED, (void*)(uintptr_t)i);
    }
}

// 创建画笔大小选择按钮
static void create_size_buttons(lv_obj_t *parent)
{
    lv_obj_t *btn;
    int sizes[] = {5, 10, 15};
    int y_pos[] = {236, 300, 364};
    
    for(int i = 0; i < 3; i++) {
        btn = lv_btn_create(parent);
        lv_obj_set_size(btn, 64, 64);
        lv_obj_set_pos(btn, 960, y_pos[i]);
        lv_obj_set_style_bg_color(btn, colors[12], LV_PART_MAIN | LV_STATE_DEFAULT);
        lv_obj_set_style_bg_opa(btn, LV_OPA_COVER, LV_PART_MAIN | LV_STATE_DEFAULT);
        
        lv_obj_t *label = lv_label_create(btn);
        char size_str[10];
        snprintf(size_str, sizeof(size_str), "%d", sizes[i]);
        lv_label_set_text(label, size_str);
        lv_obj_center(label);
        
        lv_obj_add_event_cb(btn, size_btn_event_cb, LV_EVENT_CLICKED, (void*)(uintptr_t)sizes[i]);
    }
}

// 创建橡皮擦按钮
static void create_eraser_button(lv_obj_t *parent)
{
    ui_eraserBtn = lv_btn_create(parent);
    lv_obj_set_size(ui_eraserBtn, 64, 64);
    lv_obj_set_pos(ui_eraserBtn, 960, 428);
    lv_obj_set_style_bg_color(ui_eraserBtn, lv_color_hex(0xCCCCCC), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_opa(ui_eraserBtn, LV_OPA_COVER, LV_PART_MAIN | LV_STATE_DEFAULT);
    
    lv_obj_t *label = lv_label_create(ui_eraserBtn);
    lv_label_set_text(label, "E");
    lv_obj_center(label);
    
    lv_obj_add_event_cb(ui_eraserBtn, eraser_btn_event_cb, LV_EVENT_CLICKED, NULL);
}

// 颜色按钮回调
static void color_btn_event_cb(lv_event_t *e)
{
    color_now = (int)(uintptr_t)lv_event_get_user_data(e);
    is_erasing = false;
    if(is_erasing) {
        lv_obj_set_style_bg_color(ui_eraserBtn, lv_color_hex(0xFF0000), LV_PART_MAIN | LV_STATE_DEFAULT);
    } else {
        lv_obj_set_style_bg_color(ui_eraserBtn, lv_color_hex(0xCCCCCC), LV_PART_MAIN | LV_STATE_DEFAULT);
    }
}

// 画笔大小按钮回调
static void size_btn_event_cb(lv_event_t *e)
{
    pen_size = (int)(uintptr_t)lv_event_get_user_data(e);
    is_erasing = false;
    if(is_erasing) {
        lv_obj_set_style_bg_color(ui_eraserBtn, lv_color_hex(0xFF0000), LV_PART_MAIN | LV_STATE_DEFAULT);
    } else {
        lv_obj_set_style_bg_color(ui_eraserBtn, lv_color_hex(0xCCCCCC), LV_PART_MAIN | LV_STATE_DEFAULT);
    }
}

// 橡皮擦按钮回调
static void eraser_btn_event_cb(lv_event_t *e)
{
    is_erasing = !is_erasing;
    lv_obj_t *btn = lv_event_get_target(e);
    
    if(is_erasing) {
        lv_obj_set_style_bg_color(btn, lv_color_hex(0xFF0000), LV_PART_MAIN | LV_STATE_DEFAULT);
    } else {
        lv_obj_set_style_bg_color(btn, lv_color_hex(0xCCCCCC), LV_PART_MAIN | LV_STATE_DEFAULT);
    }
}



static void canvas_event_cb(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t *ca = lv_event_get_target(e);
    lv_indev_t *indev = lv_indev_get_act();
    lv_point_t point;
    
    if(code == LV_EVENT_PRESSED || code == LV_EVENT_PRESSING) {
        lv_indev_get_point(indev, &point);
        
        // 获取画布位置
        lv_area_t canvas_area;
        lv_obj_get_coords(ca, &canvas_area);
        
        // 转换坐标到画布坐标系
        point.x -= canvas_area.x1;
        point.y -= canvas_area.y1;
        
        if(code == LV_EVENT_PRESSED) {
            last_point.x = point.x;
            last_point.y = point.y;
            draw_point(ca, point.x, point.y);
            printf("开始绘画!\n");
        }
        else if(code == LV_EVENT_PRESSING) {
            if(last_point.x != -1 && last_point.y != -1) {
                draw_line(ca, last_point.x, last_point.y, point.x, point.y);
                printf("记录绘画路径!\n");
            }
            last_point.x = point.x;
            last_point.y = point.y;
        }
    }
    else if(code == LV_EVENT_RELEASED) {
        last_point.x = -1;
        last_point.y = -1;
    }
}


// 绘制点 - 使用像素操作
static void draw_point(lv_obj_t *ca, lv_coord_t x, lv_coord_t y)
{
    // 检查坐标是否在画布范围内
    if(x < 0 || x >= 960 || y < 0 || y >= 555) return;
    
    lv_color_t color = is_erasing ? lv_color_hex(0xFFFFFF) : colors[color_now];
    
    // 绘制圆形点
    int radius = pen_size / 2;
    for(int i = -radius; i <= radius; i++) {
        for(int j = -radius; j <= radius; j++) {
            // 计算点到圆心的距离
            if(i*i + j*j <= radius*radius) {
                int px = x + i;
                int py = y + j;
                if(px >= 0 && px < 960 && py >= 0 && py < 555) {
                    lv_canvas_set_px(ca, px, py, color, LV_OPA_COVER);
                }
            }
        }
    }
}

// 绘制线 - 使用Bresenham算法
static void draw_line(lv_obj_t *ca, lv_coord_t x1, lv_coord_t y1, lv_coord_t x2, lv_coord_t y2)
{
    int dx = abs(x2 - x1);
    int dy = abs(y2 - y1);
    int sx = (x1 < x2) ? 1 : -1;
    int sy = (y1 < y2) ? 1 : -1;
    int err = dx - dy;
    
    while(1) {
        draw_point(ca, x1, y1);
        
        if(x1 == x2 && y1 == y2) break;
        
        int e2 = 2 * err;
        if(e2 > -dy) {
            err -= dy;
            x1 += sx;
        }
        if(e2 < dx) {
            err += dx;
            y1 += sy;
        }
    }
}


static void ui_event_ImgButtonPenToDet_cb(lv_event_t *e)
{
    lv_event_code_t event_code = lv_event_get_code(e);
    if(event_code == LV_EVENT_CLICKED) {
        uint32_t child_cnt = lv_obj_get_child_cnt(lv_screen_active());
        
        for(int i = child_cnt - 1; i >= 0; i--) {
            lv_obj_t *child = lv_obj_get_child(lv_screen_active(), i);
            if(child) {
                lv_obj_del(child);
            }
        }
        
        canvas = NULL;
        ui_ImgButtonPenToDet = NULL;
        
        _ui_screen_change(&ui_ScreenDestop, LV_SCR_LOAD_ANIM_FADE_ON, 100, 0, &ui_ScreenDestop_screen_init);
    }
}

void drawAPP(void)
{
    // 初始化颜色数组
    init_colors();
    
    // 获取当前屏幕
    lv_obj_t *screen = lv_screen_active(); // ui_ScreenPen
    
    // 创建画布
    canvas = lv_canvas_create(screen);
    lv_canvas_set_buffer(canvas, cbuf, 960, 555, LV_COLOR_FORMAT_RGB888);
    lv_obj_set_pos(canvas, 0, 0);
    lv_obj_set_size(canvas, 960, 555);
    lv_obj_add_flag(canvas, LV_OBJ_FLAG_CLICKABLE);
    
    // 填充白色背景
    lv_canvas_fill_bg(canvas, lv_color_hex(0xFFFFFF), LV_OPA_COVER);
    
    // 添加事件处理
    lv_obj_add_event_cb(canvas, canvas_event_cb, LV_EVENT_ALL, NULL);

    // 创建控制按钮
    create_color_buttons(screen);
    create_size_buttons(screen);
    create_eraser_button(screen);

    // 创建返回按钮
    ui_ImgButtonPenToDet = lv_imagebutton_create(screen);
    lv_imagebutton_set_src(ui_ImgButtonPenToDet, LV_IMAGEBUTTON_STATE_RELEASED, NULL, &ui_img_last3_png, NULL);
    lv_imagebutton_set_src(ui_ImgButtonPenToDet, LV_IMAGEBUTTON_STATE_PRESSED, NULL, &ui_img_last2_png, NULL);
    lv_obj_set_height(ui_ImgButtonPenToDet, 50);
    lv_obj_set_width(ui_ImgButtonPenToDet, LV_SIZE_CONTENT);
    lv_obj_set_x(ui_ImgButtonPenToDet, 474);
    lv_obj_set_y(ui_ImgButtonPenToDet, -267);
    lv_obj_set_align(ui_ImgButtonPenToDet, LV_ALIGN_CENTER);
    lv_obj_add_event_cb(ui_ImgButtonPenToDet, ui_event_ImgButtonPenToDet_cb, LV_EVENT_ALL, NULL);
}
